#include "RsGisLibClbRaditoTOARefl.h"
#include "ExtensionManager.h"
#include "ExtensionSession.h"
#include <QVariant>

RsGisLibClbRaditoTOARefl::RsGisLibClbRaditoTOARefl(RsgisPortal* ClientPortal, QWidget *parent)
{
	ui = new Ui::RsGisLibClbRaditoTOARefl();
	ui->setupUi(this);

	Portal = ClientPortal;

	connect(ui->pushButton_InputFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_InputFilePath_clicked()));
	connect(ui->pushButton_OutputFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_OutputFilePath_clicked()));
	connect(ui->pushButton_RaditoTOARefl, SIGNAL(clicked()), this, SLOT(on_action_Button_RaditoTOARefl_clicked()));

	ui->textBrowser_ParameterDescription->setText(
		"Band Num - Index of Band Num from 1\nScale Factor - is a float which can be used to scale the output pixel values(e.g., multiple by 1000), set as 1 if not wanted\nSolar Dist - is a float specifying the solar - earth distance(see imagecalibration.calc_solar_distance)\nSolar Zenith - is a a float with the solar zenith in degrees at the time of the acquisition(note 90 - solarElevation = solarZenith)\nSolar Irradiance - is a sequence of floats each with the name 'irradiance'(ESUN) which is in order of the bands in the input image\nGdal Format - string specifying the output image format\nDatatype - Specifying the output image pixel data type"
	);

	ui->textBrowser_ParameterDescription->setEnabled(false);

	QStringList DatatypeList;
	DatatypeList.append("undefined");
	DatatypeList.append("8 Int");
	DatatypeList.append("16 Int");
	DatatypeList.append("32 Int");
	DatatypeList.append("64 Int");
	DatatypeList.append("8 uInt");
	DatatypeList.append("16 uInt");
	DatatypeList.append("32 uInt");
	DatatypeList.append("64 uInt");
	DatatypeList.append("32 Float");
	DatatypeList.append("64 Float");
	ui->comboBox_Datatype->addItems(DatatypeList);

	ui->lineEdit_InputFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\LT51200352005057BJC01.tif");
	ui->lineEdit_OutputFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\TestRaditoTOARefl.tif");
	ui->lineEdit_BandNum->setText("1");
	ui->lineEdit_ScaleFactor->setText("0.0");
	ui->lineEdit_SolarZenith->setText("0.0");
	ui->lineEdit_Year->setText("2000");
	ui->lineEdit_Month->setText("9");
	ui->lineEdit_Day->setText("30");
	ui->lineEdit_GdalFormat->setText("GTIFF");
	ui->lineEdit_SolarIrradiance->setText("0.0");
	ui->comboBox_Datatype->setCurrentIndex(10);

	ui->lineEdit_Priority->setText("16");
}

void RsGisLibClbRaditoTOARefl::on_action_Button_InputFilePath_clicked()
{
	QString str = QFileDialog::getSaveFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_InputFilePath->setText(str.toUtf8());
}

void RsGisLibClbRaditoTOARefl::on_action_Button_OutputFilePath_clicked()
{
	QString str = QFileDialog::getSaveFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_OutputFilePath->setText(str.toUtf8());
}

void RsGisLibClbRaditoTOARefl::on_action_Button_RaditoTOARefl_clicked()
{
	{
		if (ui->lineEdit_InputFilePath->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Input File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_OutputFilePath->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Output File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_BandNum->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Band Num is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_Year->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Year is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_Month->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Month is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_Day->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Day is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_ScaleFactor->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Scale Factor is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_SolarZenith->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Solar Zenith is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_SolarIrradiance->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Solar Irradiance is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_Priority->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Priority is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}
	}

	QList<QVariant> SysParaList;
	SysParaList << QVariant(QString("RsGisLib"));
	SysParaList << QVariant(QString("imageCalibration"));
	SysParaList << QVariant(int(ui->lineEdit_Priority->text().toInt()));

	QList<QVariant> AlgParaList;
	AlgParaList << QVariant(QString("RaditoTOARefl"));
	AlgParaList << QVariant(QString(ui->lineEdit_InputFilePath->text()));
	AlgParaList << QVariant(QString(ui->lineEdit_OutputFilePath->text()));
	AlgParaList << QVariant(int(ui->lineEdit_BandNum->text().toInt()));
	AlgParaList << QVariant(double(ui->lineEdit_ScaleFactor->text().toDouble()));
	AlgParaList << QVariant(double(ui->lineEdit_SolarZenith->text().toDouble()));
	AlgParaList << QVariant(double(ui->lineEdit_SolarIrradiance->text().toDouble()));
	AlgParaList << QVariant(int(ui->lineEdit_Year->text().toInt()));
	AlgParaList << QVariant(int(ui->lineEdit_Month->text().toInt()));
	AlgParaList << QVariant(int(ui->lineEdit_Day->text().toInt()));
	AlgParaList << QVariant(QString(ui->lineEdit_GdalFormat->text()));
	AlgParaList << QVariant(int(ui->comboBox_Datatype->currentIndex()));

	QVariantList ArgList;
	ArgList << (QVariant)SysParaList;
	ArgList << (QVariant)AlgParaList;

	Portal->goAhead(ArgList);
}
